var Shader = Class.extend({
	init: function(context, vertexShaderCode, fragmentShaderCode) {
		var gl = context.renderer.gl;
		this.vertexShader = this.compileShader(gl, gl.VERTEX_SHADER, vertexShaderCode);
		this.fragmentShader = this.compileShader(gl, gl.FRAGMENT_SHADER, fragmentShaderCode);
        this.shaderProgram = gl.createProgram();
        gl.attachShader(this.shaderProgram, this.vertexShader);
        gl.attachShader(this.shaderProgram, this.fragmentShader);
        gl.linkProgram(this.shaderProgram);
 
       if (!gl.getProgramParameter(this.shaderProgram, gl.LINK_STATUS)) {
            alert("Could not initialise shaders");
        }
 
        gl.useProgram(this.shaderProgram);
 
        this.shaderProgram.vertexPositionAttribute = gl.getAttribLocation(this.shaderProgram, "position");
        gl.enableVertexAttribArray(this.shaderProgram.vertexPositionAttribute);
		
		this.shaderProgram.textureCoordAttribute = gl.getAttribLocation(this.shaderProgram, "uv");
		gl.enableVertexAttribArray(this.shaderProgram.textureCoordAttribute);

 
        this.shaderProgram.modelViewProjMatrix = gl.getUniformLocation(this.shaderProgram, "modelViewProjMatrix");
		this.shaderProgram.textureDiffuse = gl.getUniformLocation(this.shaderProgram, "texDiffuse");
	},

	compileShader: function(gl, shaderType, shaderCode) {
        shader =  gl.createShader(shaderType);
		gl.shaderSource(shader, shaderCode);
		gl.compileShader(shader);
		if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
			alert(gl.getShaderInfoLog(shader));
			return null;
        }
		
		return shader;
	},
	
	_setModelViewProjMatrix: function(gl, modelViewProjMatrix) {
        gl.uniformMatrix4fv(this.shaderProgram.modelViewProjMatrix, false, modelViewProjMatrix);
	},
	
	// Set the texture in the fragment shader
	_setTexture: function(gl, texture) {
		//if (texture.isLoaded()) {
			texture.bind(gl);
			gl.uniform1i(this.shaderProgram.textureDiffuse, 0);
		//}
	},
	
}); 
